<style type="text/css">
  .x-grid-record-red table{background-color: #FF7B47;}
  .x-grid-record-yellow table{background-color: #FFBB47;}
</style>

<script type="text/javascript">
  var newDailyWin;
  var editDailyWin;
  var dailyDS;
  var trainId                                                               //用做右键菜单的render，本页内传值用，要使用全局变量
  var dailyId                                                               //用做右键菜单的render，本页内传值用，要使用全局变量
      
  Ext.onReady(function(){

    var dateDS = new Ext.data.Store({
      proxy:new Ext.data.HttpProxy({url:'/daily/getDates'}),
      autoLoad:true,
      reader:new Ext.data.JsonReader({
        totalProperty: 'totalProperty',
        root: 'root'
      }, [
        {name:'date'}
      ])
    });
    var dateCombo = new Ext.form.ComboBox({
      store: dateDS,
      hiddenName:'date',
      displayField:'date',
      valueField:'date',
      typeAhead: true,
      disable:true,
      minChars:1,
      readOnly:false,
      width:140,
      mode: 'remote',
      fieldLabel:'日期选择',
      forceSelection: true,
      triggerAction: 'all',
      emptyText:'请选择日期.....',
      selectOnFocus:true,
      listeners:{                                                           //设置监听，一旦选择一个日期，就进行store的数据更替
        select : function(combo, record, index){
          dailyDS.load({
            params:{
              date:combo.value
            }
          })
        }
      }

    });
        
    //日报管理---------------------------------------------------------
    dailyDS = new Ext.data.Store({
      proxy: new Ext.data.HttpProxy({
        url: '/daily/getDailies'
      }),
      reader:new Ext.data.JsonReader({
        totalProperty: 'total',
        root:'root'
      },[
        {name:'id'},
        {name:'trainSn',mapping:'sn'},
        {
          name:'trainId',
          mapping:'train',
          convert:function(value){
            if(value){
              return value.id
            }else{
              return '未设置'
            }
          }
        },
        {name:'date'},
        {name:'last_length'},
        {name:'now_length'},
        {name:'all_length'},
        {name:'xx_length'},
        {name:'ts_length'},
        {name:'two_level_fix_length'},
        {name:'threex_length'},
        {name:'fourx_length'},
        {name:'train_number'},
        {name:'line'},
        {name:'length'}
      ]
    )
    });

    var trainSM = new Ext.grid.CheckboxSelectionModel();
    var trainCM = new Ext.grid.ColumnModel([
      new Ext.grid.RowNumberer({width: 30}),trainSM,
      {header:'车列',sortable:true,dataIndex:'trainSn',width:120},
      {header:'担当车次',sortable:true,dataIndex:'train_number',width:150},
      {header:'运行交路',sortable:true,dataIndex:'line',width:220},
      {header:'上次走行<br>  公里',sortable:true,dataIndex:'last_length',width:70},
      {header:'本次走行<br>  公里',sortable:true,dataIndex:'now_length',width:70},
      {header:'总走行公<br>  里数',sortable:true,dataIndex:'all_length',width:70},
      {header:'镟修后<br>公里数',sortable:true,dataIndex:'xx_length',width:60},
      {header:'探伤后<br>公里数',sortable:true,dataIndex:'ts_length',width:60},
      {header:'二级修<br>公里数',sortable:true,dataIndex:'two_level_fix_length',width:60},
      {header:'三级修<br>后公里',sortable:true,dataIndex:'threex_length',width:60},
      {header:'四级修<br>后公里',sortable:true,dataIndex:'fourx_length',width:60},
      {header:'一次走<br>行公里',sortable:true,dataIndex:'length',width:60}
    ]);

    var dailyGrid =  new Ext.grid.GridPanel({
      ds: dailyDS,
      cm: trainCM,
      sm: trainSM,
      width: 1110,
      height: screenHeight-250,
      iconCls: 'icon-grid',
      waitMsgTarget: true,
      viewConfig:{
        getRowClass : function(record,rowIndex,rowParams,store){
          if(28000<record.data.ts_length && record.data.ts_length<30000){   //探伤不能大于3万公里
            return 'x-grid-record-yellow';
          }
          if(record.data.ts_length == 30000){                               //探伤不能大于3万公里
            return 'x-grid-record-red';
          }
          if(190000<record.data.xx_length && record.data.xx_length<200000){ //镟修接近公里数时，变黄
            return 'x-grid-record-yellow';
          }
          if(record.data.xx_length >= 200000){                              //镟修大于20万公里时，变红
            return 'x-grid-record-red';
          }
          return '';
        }
      },
      tbar: ['-',{
          text: '编辑动车日报',
          width:50,
          iconCls: 'editItem',
          handler : function(){
            var records = dailyGrid.getSelectionModel().getSelections();
            if(records.length==0){
              Ext.MessageBox.alert('提示信息', '请先选中一条记录后，再编辑。');
            }else{
              //把表单添加到窗口中
              dailyId = records[0].get('id');
              editDailyWin = new Ext.Window({
                title: '编辑日报',
                layout:'fit',
                width:397,
                height:392,
                closeAction:'destroy',
                constrain:true,
                plain: true,
                modal: true,
                autoLoad: {
                  url: "/daily/editDaily?dailyId=" + dailyId,
                  scripts: true
                }
              });
              editDailyWin.on("destroy",function(){
                dailyDS.reload();
              });
              editDailyWin.resizable = false;
              editDailyWin.show();
            }
          }
        },
        '-',{
          text: '生成今日日报',
          width:50,
          iconCls: 'editItem',
          handler : function(){
            Ext.MessageBox.confirm('警告','确定生成今日日报？',
            function (button){
              if(button=='yes'){
                dailyGrid.disable();                                        //一旦开始生成日报，禁用grid，防止用户多次操作
                Ext.Ajax.request({
                  url:'/daily/generateDailies',
                  method:'post',
                  waitMsg: '正在生成日报...',
                  success:function(response,opts){
                    var data = Ext.util.JSON.decode(response.responseText);
                    if(data.success&&data.info=='success') {
                      dailyDS.load();
                      dateCombo.setValue(data.lastedDate);                  //将最新的日期写入combo
                      Ext.Msg.alert('提示信息', '已成功生成日报。');
                    } else {
                      Ext.Msg.alert('提示信息', data.info);
                    }
                    dailyGrid.enable();                                     //生成完后，启用grid
                  }
                });
              }
            });
          }
        },'-','日报日期：',
        dateCombo,'-',
        {
          text:'下载日报',
          iconCls: 'disk',
          handler: function(){
            var date = dateCombo.value
            if(date == ''){
              alert ("请先选择日期，再进行日报下载")
            }else if(date == '没有日报记录'){
              alert ("您没有日报数据，无法下载")
            }else{
              var path = '/daily/printDailyList?date='+date;
              window.open(path,'_blank','width=1,height=1,toolbar=no,menubar=no,location=no');
            }
          }
        },
        {
          text:'删除当前日期日报',
          width:50,
          iconCls: 'remove',
          handler:function(){
            Ext.MessageBox.confirm('警告','确定当前日期的日报吗？<br><font color="red">'+dateCombo.value +'</font>',function (button){
              if(button=='yes'){
                Ext.Ajax.request({
                  url:'/daily/deleteAll',
                  method:'post',
                  waitMsg: '正在删除日报...',
                  success:function(response,opts){
                    var data = Ext.util.JSON.decode(response.responseText);
                    if(data.success&&data.info=='success') {
                      dailyDS.reload();
                      dateCombo.setValue(data.lastedDate);                //将最新的日期写入combo
                      Ext.Msg.alert('提示信息', '已成功删除日报信息。');
                    } else {
                      Ext.Msg.alert('提示信息', data.info);
                    }
                  },
                  params: {"date":dateCombo.value}
                });
              }
            });
          }
        },'-']
    });

    dailyGrid.on('rowcontextmenu', function(grid,rowindex,e){               //用于生成右键菜单
      trainSn = dailyDS.getAt(rowindex).get('trainSn')                      //在这里取得值，可以在后面的function中使用
      trainId = dailyDS.getAt(rowindex).get('trainId')
      dailyId = dailyDS.getAt(rowindex).get('id')
      e.preventDefault();
      rightClick.showAt(e.getXY());
    });

    var rMenu0 = new Ext.menu.Item({
      text:'列车停留',
      handler:function(){
        Ext.MessageBox.confirm('警告','确定使列车<font color="red">'+trainSn
          + '</font>停留？<br>',function (button){
          if(button=='yes'){
            Ext.Ajax.request({
              url:'/train/setStay',
              method:'post',
              success:function(response,opts){
                var data = Ext.util.JSON.decode(response.responseText);
                if(data.success&&data.info=='success') {
                  Ext.Msg.alert('提示信息', '列车已经设置为停留');
                } else {
                  Ext.Msg.alert('提示信息', data.info);
                }
              },
              params: {"trainId":trainId}
            });
            dailyDS.reload();
          }
        })
      },
      iconCls:'viewItem'
    })

    var rMenu1 = new Ext.menu.Item({
      text:'进行镟修',
      handler:function(){
        newTrainTaskWin = new Ext.Window({
          layout: 'fit',
          width:797,
          height:548,
          closeAction: 'destroy',
          plain: true,
          modal: true,
          constrain:true,
          title: '添加镟修任务',
          autoLoad: {
            url: "/daily/xxTask?trainId="+trainId+"&trainSn="+trainSn+"&id="+dailyId,
            scripts: true
          }
        });
        newTrainTaskWin.on("destroy",function(){
          dailyDS.reload();
        });
        newTrainTaskWin.resizable = false;
        newTrainTaskWin.show();
      },
      iconCls:'viewItem'
    })

    var rMenu2 = new Ext.menu.Item({
      text:'进行探伤',
      handler:function(){
        newTrainTaskWin = new Ext.Window({
          layout: 'fit',
          width:797,
          height:548,
          closeAction: 'destroy',
          plain: true,
          modal: true,
          constrain:true,
          title: '添加探伤任务',
          autoLoad: {
            url: "/daily/tsTask?trainId="+trainId+"&trainSn="+trainSn,
            scripts: true
          }
        });
        newTrainTaskWin.on("destroy",function(){
          dailyDS.reload();
        });
        newTrainTaskWin.resizable = false;
        newTrainTaskWin.show();
      },
      iconCls:'viewItem'
    })

    var rMenu3 = new Ext.menu.Item({
      text:'进行三级修',
      handler:function(){
        Ext.MessageBox.confirm('警告','确定对<font color="red">'+trainSn
          + '</font>进行三级修？<br>（本操作不可恢复）',function (button){
          if(button=='yes'){
            Ext.Ajax.request({
              url:'/daily/threex',
              method:'post',
              success:function(response,opts){
                var data = Ext.util.JSON.decode(response.responseText);
                if(data.success&&data.info=='success') {
                  Ext.Msg.alert('提示信息', '三级修后公里数已经清零');
                } else {
                  Ext.Msg.alert('提示信息', data.info);
                }
              },
              params: {"id":dailyId}
            });
            dailyDS.reload();
          }
        })
      },
      iconCls:'viewItem'
    })

    var rMenu4 = new Ext.menu.Item({
      text:'进行四级修',
      handler:function(){
        Ext.MessageBox.confirm('警告','确定对<font color="red">'+trainSn
          + '</font>进行四级修？<br>（本操作不可恢复）',function (button){
          if(button=='yes'){
            Ext.Ajax.request({
              url:'/daily/fourx',
              method:'post',
              success:function(response,opts){
                var data = Ext.util.JSON.decode(response.responseText);
                if(data.success&&data.info=='success') {
                  Ext.Msg.alert('提示信息', '四级修后公里数已经清零');
                } else {
                  Ext.Msg.alert('提示信息', data.info);
                }
              },
              params: {"id":dailyId}
            });
            dailyDS.reload();
          }
        })
      },
      iconCls:'viewItem'
    })

    var rightClick = new Ext.menu.Menu({
      id:'rightClickCont',
      items: [rMenu0,rMenu1,rMenu2,rMenu3,rMenu4]
    });

    dateCombo.setValue("<%=@lastedDate%>");                                 //在页面第一次载入时为tbar上的下拉框赋值，grid显示最新和下拉框显示最新是分开做的。
    dailyGrid.render('dailyListForm');
    dailyDS.load();
  })
</script>

<div id="dailyListForm"></div>

